using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RaisingStudio.Data.Entities;
using RaisingStudio.Data.Expressions;

namespace MvcApplication1.Controllers
{
    public class IssueController : Controller
    {
        private RaisingStudio.Data.Providers.DataContext dc = new RaisingStudio.Data.Providers.DataContext();

        public IssueController()
        {
            this.dc.Log = System.Console.Out;
        }
        //
        // GET: /Issue/

        public ActionResult Index()
        {
            return View(this.dc.Query<Issue>());
        }

        //
        // GET: /Issue/Details/5

        public ActionResult Details(int id)
        {
            return View(this.dc.GetData<Issue>(id));
        }

        //
        // GET: /Issue/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Issue/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Issue issue)
        {
            try
            {
                // Insert issue.
                int result = this.dc.Insert<Issue>(issue);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Issue/Edit/5

        public ActionResult Edit(int id)
        {
            return View(this.dc.GetData<Issue>(id));
        }

        //
        // POST: /Issue/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Issue issue)
        {
            try
            {
                // Update issue.
                issue.IssueID = id;
                int result = this.dc.Update<Issue>(issue);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id)
        {
            try
            {
                // Delete issue.
                int result = this.dc.Delete<Issue>(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string Filter)
        {
            try
            {
                // Filter.
                string value = string.Format("%{0}%", Filter);
                IEnumerable<Issue> query = this.dc.Query<Issue>(Issue._.Title.Like(value) | Issue._.Description.Like(value));
                return View("Index", query);
            }
            catch
            {
                return View();
            }
        }
    }
}
